package com.chang.demo.utils;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.chang.demo.bean.FileDtoBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.function.Consumer;

@Component
public class MyInitTask {

    @Value("${controller.parser.path}")
    public String controllerPath;

    @Resource
    private ControllerParser controllerParser;

    @PostConstruct
    public void init() {
        // 执行只执行一次的初始化任务
        String newPath = String.join("/", controllerPath.split("\\."));
        String path = Paths.get("src/main/java/" + newPath).toAbsolutePath().toString();

        ArrayList<FileDtoBean> fileArrayList = FileUtil.getFilesByDictionary(path);
        fileArrayList.forEach(new Consumer<FileDtoBean>() {
            @Override
            public void accept(FileDtoBean dtoBean) {
                try {
                    controllerParser.parse(dtoBean.getFile(), dtoBean.getRelativePath());
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
        });

    }
}
